// i.d(t, { n: () => TagsViewData })
import Data from "../object/Data"
import ObservableValue, { createObVal } from "../observer/observable.value"
import ObservableArray, { createArray } from "../observer/observable.array"
import toolEnum from "../enum/tool.enum"
import MattertagsData from "./MattertagsData"
import AttachmentsData from "./AttachmentsData"
import Mattertag from "../object/Mattertag"
export default class TagsViewData extends Data {
  data: MattertagsData
  attachmentsData: AttachmentsData
  backgroundTexture: THREE.Texture
  maskTexture: THREE.Texture
  tagCreation: boolean
  tagSelectedOnly: boolean
  allTagViews: ObservableArray<any>
  enabledTagViews: ObservableArray<any>
  openTagViewObservable: ObservableValue<any>
  tagToolStateObservable: ObservableValue<string>
  listIndexObservable: ObservableValue<number>
  constructor(data: MattertagsData, attachmentsData: AttachmentsData, backgroundTexture: THREE.Texture, maskTexture: THREE.Texture) {
    super()
    this.data = data
    this.attachmentsData = attachmentsData
    this.backgroundTexture = backgroundTexture
    this.maskTexture = maskTexture
    this.name = "tags-view-data"
    this.tagCreation = !1
    this.tagSelectedOnly = !1
    this.allTagViews = createArray([])
    this.enabledTagViews = createArray([])
    this.openTagViewObservable = createObVal(null)
    this.tagToolStateObservable = createObVal(toolEnum.CLOSED)
    this.listIndexObservable = createObVal(-1)
    this.updateViewData()
  }
  getData() {
    return this.data
  }
  getEnabledTagViews() {
    return this.enabledTagViews
  }
  getAllTagViews() {
    return this.allTagViews
  }
  getTagCount() {
    return this.data.collection.length
  }
  getTagView(e) {
    const t = this.allTagViews.findIndex(t => t.id === e)
    return -1 !== t ? this.allTagViews.get(t) : null
  }
  getCurrentTagViewByIndex(e) {
    return -1 !== e ? this.enabledTagViews.get(e) : null
  }
  updateViewData() {
    const e = []
    this.data.iterate(t => {
      const i = this.createTagView(t)
      e.push(i)
    })
    this.allTagViews.replace(e)
    this.updateEnabledTagViews()
  }
  getFilteredTags() {
    const e = []
    return (
      this.allTagViews.forEach(t => {
        t.enabled && e.push(t)
      }),
      e
    )
  }
  updateEnabledTagViews() {
    const e = this.getFilteredTags().sort((e, t) => t.created.getTime() - e.created.getTime())
    this.enabledTagViews.replace(e)
  }
  createTagView(e: Mattertag) {
    return {
      id: e.sid,
      created: e.created,
      modified: e.modified,
      label: e.label,
      description: e.description,
      enabled: e.enabled,
      color: `#${e.color.getHexString()}`,
      anchorPosition: e.anchorPosition,
      floorId: e.floorId,
      roomId: e.roomId,
      stemNormal: e.stemVector,
      stemLength: e.stemHeight,
      stemEnabled: e.stemVisible,
      widgetFrame: e.widgetFrame,
      attachments: this.attachmentsData.getTagAttachments(e.sid),
      backgroundTexture: this.backgroundTexture,
      maskTexture: this.maskTexture
    }
  }
  get creatingTag() {
    return this.tagCreation
  }
  setCreatingTag(e) {
    this.tagCreation = e
  }
  get selectedPreview() {
    return this.tagSelectedOnly
  }
  setTagsToolState(e) {
    this.tagToolStateObservable.value = e
  }
  get tagsToolState() {
    return this.tagToolStateObservable.value
  }
  onTagsToolStateChanged(e) {
    return this.tagToolStateObservable.onChanged(e)
  }
  setListIndex(e) {
    this.listIndexObservable.value = e
  }
  get listIndex() {
    return this.listIndexObservable.value
  }
  onListIndexChanged(e) {
    return this.listIndexObservable.onChanged(e)
  }
  setOpenTagView(e, t = !1) {
    e === this.openTagViewObservable.value && this.tagSelectedOnly !== t
      ? ((this.tagSelectedOnly = t), this.openTagViewObservable.setDirty())
      : ((this.tagSelectedOnly = t), (this.openTagViewObservable.value = e))
  }
  get openTagView() {
    return this.openTagViewObservable.value
  }
  onOpenTagViewChanged(e) {
    return this.openTagViewObservable.onChanged(e)
  }
  updateOpenTagView(e) {
    const t = this.openTagView
    if (t) {
      const i = Object.assign(Object.assign({}, t), e)
      this.setOpenTagView(i)
    }
  }
}
